package com.shjn.squirrel.customView;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.shjn.squirrel.R;
import com.shjn.squirrel.utils.SoundPlayUtils;

/**
 * 自定义用户数字键盘
 */
public class UserDigitalKeyboard extends RelativeLayout implements View.OnClickListener {

    /**
     * 与键盘绑定的editview
     */
    private EditText mEditText;

    private static final int Tel_Num_Count = 13;

    public UserDigitalKeyboard(Context context) {
        super(context);
        initView();
    }

    public UserDigitalKeyboard(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    /**
     * 加载键盘布局文件，初始化相关控件
     */
    private void initView() {
        View keybordView = inflate(getContext(), R.layout.user_digital_keyboard, this);
        keybordView.findViewById(R.id.btn_num_0).setOnClickListener(this);
        keybordView.findViewById(R.id.btn_num_1).setOnClickListener(this);
        keybordView.findViewById(R.id.btn_num_2).setOnClickListener(this);
        keybordView.findViewById(R.id.btn_num_3).setOnClickListener(this);
        keybordView.findViewById(R.id.btn_num_4).setOnClickListener(this);
        keybordView.findViewById(R.id.btn_num_5).setOnClickListener(this);
        keybordView.findViewById(R.id.btn_num_6).setOnClickListener(this);
        keybordView.findViewById(R.id.btn_num_7).setOnClickListener(this);
        keybordView.findViewById(R.id.btn_num_8).setOnClickListener(this);
        keybordView.findViewById(R.id.btn_num_9).setOnClickListener(this);
        keybordView.findViewById(R.id.btn_del).setOnClickListener(this);
        keybordView.findViewById(R.id.btn_clear).setOnClickListener(this);
    }

    /**
     * 绑定editText
     *
     * @param editText
     */
    public void setEditText(EditText editText) {
        mEditText = editText;
    }

    /**
     * 按钮点击事件
     *
     * @param view
     */
    @Override
    public void onClick(View view) {
        if (mEditText != null) {
            switch (view.getId()) {
                case R.id.btn_num_0:
                    if (mEditText.getText().length() < Tel_Num_Count) {
                        SoundPlayUtils.play(0);
                        mEditText.getText().insert(mEditText.getSelectionStart(), "0");
                    }
                    break;
                case R.id.btn_num_1:
                    if (mEditText.getText().length() < Tel_Num_Count) {
                        SoundPlayUtils.play(1);
                        mEditText.getText().insert(mEditText.getSelectionStart(), "1");
                    }
                    break;
                case R.id.btn_num_2:
                    if (mEditText.getText().length() < Tel_Num_Count) {
                        SoundPlayUtils.play(2);
                        mEditText.getText().insert(mEditText.getSelectionStart(), "2");
                    }
                    break;
                case R.id.btn_num_3:
                    if (mEditText.getText().length() < Tel_Num_Count) {
                        SoundPlayUtils.play(3);
                        mEditText.getText().insert(mEditText.getSelectionStart(), "3");
                    }
                    break;
                case R.id.btn_num_4:
                    if (mEditText.getText().length() < Tel_Num_Count) {
                        SoundPlayUtils.play(4);
                        mEditText.getText().insert(mEditText.getSelectionStart(), "4");
                    }
                    break;
                case R.id.btn_num_5:
                    if (mEditText.getText().length() < Tel_Num_Count) {
                        SoundPlayUtils.play(5);
                        mEditText.getText().insert(mEditText.getSelectionStart(), "5");
                    }
                    break;
                case R.id.btn_num_6:
                    if (mEditText.getText().length() < Tel_Num_Count) {
                        SoundPlayUtils.play(6);
                        mEditText.getText().insert(mEditText.getSelectionStart(), "6");
                    }
                    break;
                case R.id.btn_num_7:
                    if (mEditText.getText().length() < Tel_Num_Count) {
                        SoundPlayUtils.play(7);
                        mEditText.getText().insert(mEditText.getSelectionStart(), "7");
                    }
                    break;
                case R.id.btn_num_8:
                    if (mEditText.getText().length() < Tel_Num_Count) {
                        SoundPlayUtils.play(8);
                        mEditText.getText().insert(mEditText.getSelectionStart(), "8");
                    }
                    break;
                case R.id.btn_num_9:
                    if (mEditText.getText().length() < Tel_Num_Count) {
                        SoundPlayUtils.play(9);
                        mEditText.getText().insert(mEditText.getSelectionStart(), "9");
                    }
                    break;
                case R.id.btn_del:
                    if (mEditText.getSelectionStart() > 0) {
                        mEditText.getText().delete(mEditText.getSelectionStart() - 1, mEditText.getSelectionStart());
                    }
                    break;
                case R.id.btn_clear:
                    mEditText.getText().clear();
                    break;
                default:
                    break;
            }
        }
    }
}
